use std::fmt;
use std::fmt::{Display, Formatter};

struct City {
    name: &'static str,
    lat: f32,
    lon: f32,
}

impl Display for City {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let lat_c = if self.lat >= 0.0 { 'N' } else { 'S' };
        let lon_c = if self.lon >= 0.0 { 'E' } else { 'W' };

        // `write!` 和 `format!` 类似，但它会将格式化后的字符串写入
        // 一个缓冲区（即第一个参数f）中。
        write!(f, "{}: {:.3}°{} {:.3}°{}",
               self.name, self.lat.abs(), lat_c, self.lon.abs(), lon_c)
    }
}

impl City {
    pub fn new(name: &'static str, lat: f32, lon: f32) -> City {
        City { name, lat, lon }
    }
}

#[derive(Debug)]
struct Color {
    red: u8,
    green: u8,
    blue: u8,
}

fn main() {
    for city in [
        City::new("上海", 12.1, 34.4),
        City::new("北京", 12.1, 34.4),
        City::new("天津", 12.1, 34.4),
    ].iter() {
        println!("{}", *city)
    }
}